﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Dia2Lib;

namespace Kokomo.Mirror.PeFormat.Symbols
{
	abstract class DiaEnumerator<TCom, TItem> : SymbolBase, IEnumerator<TItem>
		where TItem : class
	{
		public DiaEnumerator(
			IEnumUnknown enumUnknown,
			SymbolSource source
			)
			: base(source)
		{
			if (enumUnknown == null) throw new ArgumentNullException("enumUnknown");
			this._enumUnknown = new ComWrapper<IEnumUnknown>(enumUnknown);
		}

		private ComWrapper<IEnumUnknown> _enumUnknown;
		private IEnumUnknown EnumUnknown { get { return this._enumUnknown.Object; } }

		private TItem _current;
		public TItem Current
		{
			get { return this._current; }
		}

		public void Dispose()
		{
		}

		object System.Collections.IEnumerator.Current
		{
			get { return this._current; }
		}

		public bool MoveNext()
		{
			object obj;
			uint fetched;
			this.EnumUnknown.RemoteNext(1, out obj, out fetched);
			if (fetched != 1)
			{
				this._current = null;
				return false;
			}

			this._current = this.CreateObject((TCom)obj);
			return true;
		}

		protected abstract TItem CreateObject(TCom comObject);

		public void Reset()
		{
			this.EnumUnknown.Reset();
		}
	}
}
